//
// Created by Felix on 24-5-16.
//

#ifndef GAZE_CAMERADB_HPP
#define GAZE_CAMERADB_HPP

#include "oatpp-sqlite/orm.hpp"

#include "model/CameraModel.hpp"

#include OATPP_CODEGEN_BEGIN(DbClient)

class CameraDb : public oatpp::orm::DbClient {
public:
    explicit CameraDb(
            const std::shared_ptr<oatpp::orm::Executor> &executor
    ) : oatpp::orm::DbClient(executor) {
        oatpp::orm::SchemaMigration migration(executor);
        auto version = executor->getSchemaVersion();
        OATPP_LOGD("CameraDb", "Migration - OK. Version=%lld.", version)
    }

    QUERY(createCamera,
          "INSERT INTO camera "
          "(title, pull_url, push_url, model, status, create_time, update_time, delete_time) VALUES "
          "(:camera.title, :camera.pull_url, :camera.push_url, :camera.model, :camera.status, :camera.create_time, :camera.update_time, :camera.delete_time) "
          "RETURNING *;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::Object<CameraModel>, camera))

    QUERY(updateCameraDetails,
          "UPDATE camera SET "
          "title = :camera.title, "
          "pull_url = :camera.pull_url, "
          "push_url = :camera.push_url, "
          "model = :camera.model, "
          "status = :camera.status, "
          "update_time = :camera.update_time "
          "WHERE id = :camera.id "
          "RETURNING *;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::Object<CameraModel>, camera))

    QUERY(updateCameraStatus,
          "UPDATE camera SET "
          "status = :camera.status, "
          "update_time = :camera.update_time "
          "where id = :camera.id "
          "RETURNING *;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::Object<CameraModel>, camera))

    QUERY(getCameraById,
          "SELECT * FROM camera "
          "WHERE id = :camera_id;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::Int32, cameraId, "camera_id"))

    QUERY(getAllCamera,
          "SELECT id as camera_id, title, pull_url, push_url, model, status, create_time, update_time, delete_time FROM camera "
          "ORDER BY id DESC "
          "LIMIT :limit OFFSET :offset;",
          PARAM(oatpp::Int32, offset),
          PARAM(oatpp::Int32, limit))

    QUERY(countAllCamera, "SELECT COUNT(id) FROM camera where 1;")
};

#include OATPP_CODEGEN_END(DbClient)

#endif //GAZE_CAMERADB_HPP
